#include <stdlib.h>
#include <stdio.h>
#include "queue.h"

// typedef int ElementType;
// struct queue {
// 	int capacity;
// 	int size;
// 	int front;
// 	int rare;
// 	ElementType *array;
// };
// typedef struct queue* Queue;

int isEmpty(Queue queue) {
	return queue->size == 0;
}

int isFull(Queue queue) {
	return queue->size == queue->capacity;
}

Queue createQueue(int capacity) {
	Queue queue = malloc(sizeof(struct queue));
	queue->capacity  = capacity;
	queue->size = 0;
	queue->front = 0;
	queue->rare = 0;
	queue->array = malloc(sizeof(ElementType) * capacity);
	return queue;
}

void makeEmpty(Queue queue) {
	queue->size = 0;
	queue->front = 0;
	queue->rare = 0;
}

void enQueue(Queue queue, ElementType x) {
	if (isFull(queue)) {
		printf("queue is full \n");
		return;
	}

	queue->array[queue->rare] = x;
	queue->rare ++;
	queue->size ++;
	if (queue->rare == queue->capacity) {
		queue->rare = 0;
	}
}

ElementType front(Queue queue) {
	if (isEmpty(queue)) {
		perror("queue is empty");
	}

	return queue->array[queue->front];
}

ElementType deQueue(Queue queue) {
	if (isEmpty(queue)) {
		perror("queue is empty");
	}

	ElementType ele = queue->array[queue->front];
	queue->front ++;
	queue->size --;
	if (queue->front == queue->capacity) {
		queue->front = 0;
	}
	return ele;
}

void disposeQueue(Queue queue) {
	int i;
	free(queue->array);
	free(queue);
}

